#!/bin/sh
# postrm script for atnd-ov
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postrm> `remove'
#        * <postrm> `purge'
#        * <old-postrm> `upgrade' <new-version>
#        * <new-postrm> `failed-upgrade' <old-version>
#        * <new-postrm> `abort-install'
#        * <new-postrm> `abort-install' <old-version>
#        * <new-postrm> `abort-upgrade' <old-version>
#        * <disappearer's-postrm> `disappear' <overwriter>
#          <overwriter-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    purge|remove|failed-upgrade|abort-install|abort-upgrade|disappear)

        ## Remove sqlite ATND user data db
        rm -f /home/user/.local/share/data/QML/OfflineStorage/Databases/6e8f2d4233eed01e05337c0ef0f8ce1b.* || true
        if [ -f "/etc/skel/.profile" ]; then
          source /etc/skel/.profile
          if [ -f "/tmp/session_bus_address.user" ]; then
              ## Remove notifications from device event feed
              qdbus com.nokia.home.EventFeed /eventfeed com.nokia.home.EventFeed.removeItemsBySourceName atnd
          fi
        fi

    ;;

    upgrade)
        ## Don't remove database on upgrade, if it really needed (DB format changed) do it then in postinst
    ;;

    *)
        echo "postrm called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

## Remove mimetypes that were added for ATND

DEFAULT_MIMETYPES_FILE='/usr/share/applications/defaults.list'
MIMETYPE_STR1='x-maemo-highlight\/atnd-url=qatnd_harmattan.desktop'
MIMETYPE_STR2='x-maemo-urischeme\/atnd=qatnd_harmattan.desktop'
if [ -f $DEFAULT_MIMETYPES_FILE ]; then
   if grep -qs $MIMETYPE_STR1 $DEFAULT_MIMETYPES_FILE
   then
        sed -i "/"$MIMETYPE_STR1"/ d"  $DEFAULT_MIMETYPES_FILE
   fi

   if grep -qs $MIMETYPE_STR2 $DEFAULT_MIMETYPES_FILE
   then
        sed -i "/"$MIMETYPE_STR2"/ d"  $DEFAULT_MIMETYPES_FILE
   fi
fi

echo "Updating desktop database"
update-desktop-database

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

exit 0
